﻿ #region Disclaimer/Info
 
 /////////////////////////////////////////////////////////////////////////////////////////////////
 //
 //   File:		CaptchaHelper.cs
 //   Website:		http://dexterblogengine.com/
 //   Authors:		http://dexterblogengine.com/About.ashx
 //   Rev:		1
 //   Created:		19/01/2011
 //   Last edit:		19/01/2011
 //   License:		GNU Library General Public License (LGPL)
 // 
 //   For updated news and information please visit http://dexterblogengine.com/
 //   Dexter is hosted to Codeplex at http://dexterblogengine.codeplex.com
 //   For any question contact info@dexterblogengine.com
 //
 ///////////////////////////////////////////////////////////////////////////////////////////////////
 
 #endregion

using System.IO;
using System.Web.Mvc;
using System.Web.UI;
using Dexter.Dependency;
using Dexter.Domain.Model;
using Dexter.Services;
using Recaptcha;

namespace Dexter.Web.Mvc.Helpers {
	public static class CaptchaHelper {
		static SiteConfiguration Configuration {
			get { return DexterContainer.Resolve <IConfigurationService> ( ).Configuration; }
		}

		public static MvcHtmlString Recaptcha ( this HtmlHelper helper ) {
			return Recaptcha ( helper , null );
		}

		public static MvcHtmlString Recaptcha ( this HtmlHelper helper, short? tabIndex ) {
			string control = string.Empty;

			if ( Configuration.AntispamConfiguration.ReCaptchaConfiguration.Enable ) {
				RecaptchaControl captchaControl = new RecaptchaControl {
					ID = "recaptcha" ,
					Theme = Configuration.AntispamConfiguration.ReCaptchaConfiguration.Theme ,
					PublicKey = Configuration.AntispamConfiguration.ReCaptchaConfiguration.PublicKey ,
					PrivateKey = Configuration.AntispamConfiguration.ReCaptchaConfiguration.PrivateKey
				};

				if ( tabIndex.HasValue ) {
					captchaControl.TabIndex = tabIndex.Value;
				}

				HtmlTextWriter htmlWriter = new HtmlTextWriter ( new StringWriter ( ) );

				captchaControl.RenderControl ( htmlWriter );
				control = htmlWriter.InnerWriter.ToString ( );
			}

			return MvcHtmlString.Create ( control );
		}
	}
}
